/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.cicp.util.pool;

import org.apache.commons.pool.PoolableObjectFactory;

import com.sinodata.bsm.cicp.util.telnet.JtaTelnet;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author wangyazhou
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-10-24 下午1:49:13          wangyazhou        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class TelnetPoolableObject implements PoolableObjectFactory {

    private String ip;

    private String user;

    private String pwd;

    private int port = 22;

    private String prompt;

    public TelnetPoolableObject(String ip, int port, String user, String pwd, String propmt) {
        this.ip = ip;
        this.port = port;
        this.user = user;
        this.pwd = pwd;
        this.prompt = propmt;
    }

    @Override
    public void activateObject(Object arg0) throws Exception {

    }

    @Override
    public void destroyObject(Object obj) throws Exception {
        JtaTelnet telnet = (JtaTelnet) obj;
        telnet.disconnect();
    }

    @Override
    public Object makeObject() throws Exception {
        System.out.println("新建一个telnet连接" + user + "-->" + pwd);
        JtaTelnet jtaTelnet = new JtaTelnet();
        jtaTelnet.connect(ip.split(":")[0], port);
        jtaTelnet.setHostIp(ip.split(":")[0]);
        jtaTelnet.runLogin(user, pwd, prompt);
        jtaTelnet.setIsValid(true);
        //jtaTelnet.runTask("bash");
        // jtaTelnet.runTask("LANG=C;export LANG");
        Thread.sleep(500L);
        return jtaTelnet;
    }

    @Override
    public void passivateObject(Object arg0) throws Exception {
    }

    @Override
    public boolean validateObject(Object obj) {
        JtaTelnet telnet = (JtaTelnet) obj;
        boolean b = telnet.getIsValid();
        return b;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getPrompt() {
        return prompt;
    }

    public void setPrompt(String prompt) {
        this.prompt = prompt;
    }

}
